package com.glb.gilibuy.service.impl;

import com.github.pagehelper.PageInfo;
import com.glb.gilibuy.dao.UserReceiveAddressDao;
import com.glb.gilibuy.entity.*;
import com.glb.gilibuy.service.UserReceiveAddressService;
import com.glb.gilibuy.util.BeanUtil;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.util.SnowflakeIdUtil;
import com.glb.gilibuy.web.vo.CartVO;
import com.glb.gilibuy.web.vo.UserReceiveAddressVO;
import com.glb.gilibuy.web.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/6 9:13
 */
@Service
public class
UserReceiveAddressServiceImpl implements UserReceiveAddressService {
    @Autowired
    private UserReceiveAddressDao userReceiveAddressDao;

    @Override
    public UserReceiveAddress selectAddressByAddressId(String addressId) {
        if (!"".equals(addressId)) {
            UserReceiveAddressExample userReceiveAddressExample = new UserReceiveAddressExample();
            userReceiveAddressExample.or().andAddressIdEqualTo(addressId).andIsDeletedEqualTo(0);
            List<UserReceiveAddress> userReceiveAddressList = userReceiveAddressDao.selectByExample(userReceiveAddressExample);
            if (userReceiveAddressList.isEmpty()) {
                return null;
            }
            return userReceiveAddressList.get(0);
        }
        return null;
    }

    @Override
    public List<UserReceiveAddress> selectAddressesByUserId(String userId) {
        if (!"".equals(userId)) {
            UserReceiveAddressExample userReceiveAddressExample = new UserReceiveAddressExample();
            userReceiveAddressExample.setOrderByClause("id desc");
            userReceiveAddressExample.or().andUserIdEqualTo(userId).andIsDeletedEqualTo(0);
            List<UserReceiveAddress> userReceiveAddressList = userReceiveAddressDao.selectByExample(userReceiveAddressExample);
            return userReceiveAddressList;
        }
        return null;
    }

    @Override
    public UserReceiveAddress selectDefaultAddress(String userId) {
        if (!"".equals(userId)) {
            UserReceiveAddressExample userReceiveAddressExample = new UserReceiveAddressExample();
            userReceiveAddressExample.or().andUserIdEqualTo(userId).andIsDefaultEqualTo(1).andIsDeletedEqualTo(0);
            List<UserReceiveAddress> userReceiveAddressList = userReceiveAddressDao.selectByExample(userReceiveAddressExample);
            if (userReceiveAddressList.isEmpty()) {
                return null;
            }
            return userReceiveAddressList.get(0);
        }
        return null;
    }

    @Override
    public UserReceiveAddressVO findDefaultAddress(String userId) {
        UserReceiveAddress userReceiveAddress = selectDefaultAddress(userId);
        if (userReceiveAddress == null) {
            return null;
        }
        UserReceiveAddressVO userReceiveAddressVO = new UserReceiveAddressVO();
        BeanUtil.copyOne(userReceiveAddress, userReceiveAddressVO);
        return userReceiveAddressVO;
    }

    @Override
    public PageInfo findAddressesByUserId(String userId) {
        List<UserReceiveAddress> userReceiveAddressList = selectAddressesByUserId(userId);
        if (userReceiveAddressList.isEmpty()) {
            return null;
        }
        PageInfo pageInfo = new PageInfo(userReceiveAddressList);
        List<UserReceiveAddressVO> userReceiveAddressVOList = BeanUtil.copyList(userReceiveAddressList, UserReceiveAddressVO.class);
        pageInfo.setList(userReceiveAddressVOList);
        return pageInfo;
    }

    @Override
    public Map<String, Object> addAddress(UserReceiveAddress userReceiveAddress) {
        if (userReceiveAddress.getIsDefault() == 1) {
            //地址默认设置
            UserReceiveAddress userDefaultAddressFromDB = selectDefaultAddress(userReceiveAddress.getUserId());
            if (userDefaultAddressFromDB != null) {
                //如果用户有默认地址，旧默认地址改为普通地址，新地址改为默认地址
                userDefaultAddressFromDB.setIsDefault(0);
                updateAddress(userDefaultAddressFromDB);
                //updateAddressIsDefault(userDefaultAddressFromDB.getAddressId(), 0);
            }
        }
        //地址添加
        userReceiveAddress.setAddressId(String.valueOf(SnowflakeIdUtil.getSnowflakeId()));
        userReceiveAddress.setGmtCreate(new Date());
        userReceiveAddress.setIsDeleted(0);
        if (userReceiveAddressDao.insertSelective(userReceiveAddress) > 0) {
            return MapGenerator.getRes(200, "添加收货地址成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> updateAddress(UserReceiveAddress userReceiveAddress) {
        UserReceiveAddress userReceiveAddressFromDB = selectAddressByAddressId(userReceiveAddress.getAddressId());
        if (userReceiveAddressFromDB == null) {
            return MapGenerator.getRes(501, "地址不存在！");
        }
        if (!userReceiveAddress.getUserId().equals(userReceiveAddressFromDB.getUserId())) {
            return MapGenerator.getRes(502, "无权限修改！");
        }
        //收件人姓名不空就改
        if (!StringUtils.isEmpty(userReceiveAddress.getReceiveName())) {
            userReceiveAddressFromDB.setReceiveName(userReceiveAddress.getReceiveName());
        }
        //收件人手机号不空就改，乱填报错
        if (!StringUtils.isEmpty(userReceiveAddress.getReceivePhone())) {
            String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
            if (!userReceiveAddress.getReceivePhone().matches(regex)) {
                return MapGenerator.getRes(503, "手机号格式有误！");
            }
            userReceiveAddressFromDB.setReceivePhone(userReceiveAddress.getReceivePhone());
        }
        //省不空就改
        if (!StringUtils.isEmpty(userReceiveAddress.getReceiveProvince())) {
            userReceiveAddressFromDB.setReceiveProvince(userReceiveAddress.getReceiveProvince());
        }
        //市不空就改
        if (!StringUtils.isEmpty(userReceiveAddress.getReceiveCity())) {
            userReceiveAddressFromDB.setReceiveCity(userReceiveAddress.getReceiveCity());
        }
        //区不空就改
        if (!StringUtils.isEmpty(userReceiveAddress.getReceiveRegion())) {
            userReceiveAddressFromDB.setReceiveRegion(userReceiveAddress.getReceiveRegion());
        }
        //详细地址不空就改
        if (!StringUtils.isEmpty(userReceiveAddress.getReceiveAddressDetail())) {
            userReceiveAddressFromDB.setReceiveAddressDetail(userReceiveAddress.getReceiveAddressDetail());
        }
        //是否为默认地址不空就改，乱填报错
        if (!StringUtils.isEmpty(userReceiveAddress.getIsDefault())) {
            Integer isDefault = userReceiveAddress.getIsDefault();
            if (isDefault == 1 || isDefault == 0) {
                userReceiveAddressFromDB.setIsDefault(isDefault);
                //使默认地址保持唯一
                if (isDefault == 1) {
                    UserReceiveAddress oldDefaultAddress = selectDefaultAddress(userReceiveAddress.getUserId());
                    if (oldDefaultAddress != null) {
                        oldDefaultAddress.setIsDefault(0);
                        updateAddress(oldDefaultAddress);
                    }
                }
            } else {
                return MapGenerator.getRes(504, "是否为默认地址参数超出范围！");
            }
        }
        // 用userReceiveAddressFromDB更新
        if (userReceiveAddressDao.updateByPrimaryKeySelective(userReceiveAddressFromDB) > 0) {
            return MapGenerator.getRes(200, "修改收货地址成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> deleteAddress(UserReceiveAddress userReceiveAddress) {
        UserReceiveAddress userReceiveAddressFromDB = selectAddressByAddressId(userReceiveAddress.getAddressId());
        if (userReceiveAddressFromDB == null) {
            return MapGenerator.getRes(500, "地址不存在！");
        }
        if (!userReceiveAddress.getUserId().equals(userReceiveAddressFromDB.getUserId())) {
            return MapGenerator.getRes(501, "无权限删除！");
        }
        userReceiveAddressFromDB.setIsDeleted(1);
        if (userReceiveAddressDao.updateByPrimaryKeySelective(userReceiveAddressFromDB) > 0) {
            return MapGenerator.getRes(200, "删除成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public int addAddressByUserReceiveAddress(UserReceiveAddress userReceiveAddress) {
        if (userReceiveAddress != null) {
            //生成地址id
            userReceiveAddress.setAddressId(String.valueOf(SnowflakeIdUtil.getSnowflakeId()));
            //激活
            userReceiveAddress.setIsDeleted(0);
            //新建时间
            userReceiveAddress.setGmtCreate(new Date());
            if (userReceiveAddressDao.insert(userReceiveAddress) > 0) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int updateAddressIsDefault(String addressId, Integer isDefault) {
        UserReceiveAddressExample example = new UserReceiveAddressExample();
        example.or().andAddressIdEqualTo(addressId).andIsDeletedEqualTo(0);
        if ((!"".equals(addressId)) && (isDefault != null)) {
            if (userReceiveAddressDao.selectByExample(example).get(0) != null) {
                UserReceiveAddress userReceiveAddress = new UserReceiveAddress();
                userReceiveAddress.setIsDefault(isDefault);
                if (userReceiveAddressDao.updateByExampleSelective(userReceiveAddress, example) > 0) {
                    return 1;
                }
            }
        }
        return 0;
    }
}
